package com.ferryc.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ferryc.commons.utils.StringUtil;
import com.ferryc.commons.web.BaseController;
import com.ferryc.constant.GlobalConstant;
import com.ferryc.constant.ResultConstant;
import com.ferryc.modules.sys.entity.SysMenu;
import com.ferryc.modules.sys.entity.SysMenu.MenuType;
import com.ferryc.modules.sys.entity.param.SysMenuParam;
import com.ferryc.modules.sys.service.ISysMenuService;

/**
 * 
 * 类名: SysMenuController <br/>
 * 作用： 控制<br/>
 * 方法： <br/>
 * 创建者: Ferry C <br/>
 * 添加时间: 2017年11月26日 04:19:13 <br/>
 */
@Controller
@RequestMapping("/menu")
public class SysMenuController extends BaseController {

	/** */
	@Autowired
	private ISysMenuService sysMenuService;

	/** */
	@RequiresPermissions("menu")
	@GetMapping("/")
	public String manage(Model model) {
		return "sys/menu/list";
	}

	/**
	 * 列表查询
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @return
	 */
	@RequiresPermissions("menu")
	@PostMapping("/list")
	@ResponseBody
	public Object list(SysMenuParam sysMenuParam) {
		sysMenuParam.setStatus(StringUtil.EMPTY);
		SysMenu sysMenu = sysMenuParam.toSysMenu();
		sysMenu.setParentIdAndId(sysMenuParam.getParentId());
		sysMenu.setParentId(StringUtil.EMPTY);
		return sysMenuService.selectPage(sysMenu);
	}

	/**
	 * 树查询
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @return
	 */
	@GetMapping("/treeList")
	@ResponseBody
	public Object treeList(SysMenuParam sysMenuParam) {
		sysMenuParam.setType(MenuType.MENU.getValue());
		return sysMenuService.selectTreeList(sysMenuParam.toSysMenu());
	}

	/**
	 * 进入新增页面
	 * 
	 * @param sysLogInfoParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 *            request
	 * @return
	 */
	@RequiresPermissions("menu_add")
	@GetMapping("/addPage")
	public Object addPage(SysMenuParam sysMenuParam, Model model, HttpServletRequest request) {
		model.addAttribute(GlobalConstant.PARENT_ID, request.getParameter(GlobalConstant.PARENT_ID));
		return "sys/menu/add";
	}

	/**
	 * 新增保存
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("menu_add")
	@PostMapping("/insert")
	@ResponseBody
	public Object insert(@Valid SysMenuParam sysMenuParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean nameFlag = sysMenuService.checkMenuName(sysMenuParam.toSysMenu());
		if (!nameFlag) {
			return renderError(ResultConstant.EXISTS_MENU_NAME);
		}
		if (StringUtil.isNotBlank(sysMenuParam.getPermission())) {
			boolean permisFlag = sysMenuService.checkPermission(sysMenuParam.toSysMenu());
			if (!permisFlag) {
				return renderError(ResultConstant.EXISTS_MENU_PERMISSION);
			}
		}
		sysMenuParam.preInsert();
		sysMenuService.insert(sysMenuParam.toSysMenu());
		return renderSuccess(ResultConstant.INSERT_SUCCESS);
	}

	/**
	 * 进入查看页面
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("menu_view")
	@GetMapping("/viewPage")
	public Object viewPage(SysMenuParam sysMenuParam, Model model, HttpServletRequest request) {
		sysMenuParam.setStatus(StringUtil.EMPTY);
		SysMenu sysMenu = sysMenuService.get(sysMenuParam.getId());
		model.addAttribute("info", sysMenu);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.TRUE);
		return "sys/menu/edit";
	}

	/**
	 * 进入修改页面
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @param model
	 *            Model
	 * @param request
	 * @return
	 */
	@RequiresPermissions("menu_update")
	@GetMapping("/editPage")
	public Object editPage(SysMenuParam sysMenuParam, Model model, HttpServletRequest request) {
		sysMenuParam.setStatus(StringUtil.EMPTY);
		SysMenu sysMenu = sysMenuService.get(sysMenuParam.getId());
		model.addAttribute("info", sysMenu);
		model.addAttribute(GlobalConstant.READ_ONLY, Boolean.FALSE);
		return "sys/menu/edit";
	}

	/**
	 * 修改
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("menu_update")
	@PostMapping("/save")
	@ResponseBody
	public Object save(@Valid SysMenuParam sysMenuParam, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			List<FieldError> errorList = bindingResult.getFieldErrors();
			return renderError(errorList);
		}
		boolean nameFlag = sysMenuService.checkMenuName(sysMenuParam.toSysMenu());
		if (!nameFlag) {
			return renderError(ResultConstant.EXISTS_MENU_NAME);
		}
		if (StringUtil.isNotBlank(sysMenuParam.getPermission())) {
			boolean permisFlag = sysMenuService.checkPermission(sysMenuParam.toSysMenu());
			if (!permisFlag) {
				return renderError(ResultConstant.EXISTS_MENU_PERMISSION);
			}
		}
		sysMenuParam.preUpdate();
		sysMenuService.update(sysMenuParam.toSysMenu());
		return renderSuccess(ResultConstant.UPDATE_SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param sysMenuParam
	 *            请求参数
	 * @param bindingResult
	 *            数据校验结果
	 * @return
	 */
	@RequiresPermissions("menu_delete")
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(SysMenuParam sysMenuParam, BindingResult bindingResult) {
		if (SysMenu.SYSTEM_ID.equals(sysMenuParam.getId())) {
			return renderError(ResultConstant.ERROR_DEFAULT_SYSTEM);
		}
		boolean flag = sysMenuService.checkChild(sysMenuParam.toSysMenu());
		if (flag) {
			return renderError(ResultConstant.EXISTS_MENU_CHILD);
		}
		sysMenuParam.preUpdate();
		sysMenuParam.setDelFlag(GlobalConstant.YES);
		sysMenuService.update(sysMenuParam.toSysMenu());
		return renderSuccess(ResultConstant.DELETE_SUCCESS);
	}

	@PostMapping("/treeMenuList")
	@ResponseBody
	public Object treeMenuList(SysMenuParam sysMenuParam) {
		return sysMenuService.selectTreeList(sysMenuParam.toSysMenu());
	}

	@PostMapping("/treeMenuPermList")
	@ResponseBody
	public Object treeMenuPermList(String permId) {
		return sysMenuService.selectTreePermList(permId);
	}

}